const router = require('koa-router')()
const mongoose = require('mongoose')

const Image = mongoose.model('Image')
const Comment = mongoose.model('Comment')
const stats = require('../helpers/stats')
const comments = require('../helpers/comments')

router.get('/', async (ctx, next) => {

    await Promise.all([
      Image.find().sort({timestamp: -1}).limit(6),
      comments(),
      Image.find().sort({likes: -1}).limit(6),
      stats()
    ])
    //  .then(doc => console.log(doc[1]))
        .then(values => ctx.render(
          'index', {
            title: '图片共享',
            images: values[0],
            comments: values[1],
            popular: values[2],
            stats: values[3]
        }))
        .catch(err => ctx.throw(500, err))
})

module.exports = router
